package com.zbkj.business.kaoshi.receive;

import com.tiangong.framework.annotation.OpenApi;
import com.tiangong.framework.annotation.OpenApiTag;
//import com.baomidou.dynamic.datasource.annotation.DS;
import com.tiangong.framework.bo.CDO;
import com.tiangong.framework.bo.Return;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import cn.hutool.core.bean.BeanUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.BaseService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import java.util.Arrays;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;

/**
 * @description: exam_receive用户领卡流程
 * Author: jason
 * @create: ${creatTime}
 **/
@Service("receive")
@Slf4j
@OpenApi
//@DS("prod")
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
@OpenApiTag(value = "用户领卡流程",tag = "${projectName}")
public class ReceiveService extends BaseService<ReceiveDao, Receive> implements IService<Receive> {

    /**
     * list
     */
    @OpenApiTag("查询用户领卡流程列表")
    public Return list(CDO cdoRequest) {
        cdoRequest.clearBlankValue();
//        cdoRequest.pageHandle();
//        List<CDO> pageList = this.baseMapper.getPageList(cdoRequest);
//        int total = this.baseMapper.getPageListCount(cdoRequest);
//        CDO cdoResult=new CDO().set("total",total).set("datalist",pageList);
        return super.getPageList(cdoRequest);
    }
    /**
     * info
     */
    @OpenApiTag("查询用户领卡流程信息")
    public Return info(CDO cdoRequest) {
        Receive bo = BeanUtil.toBean(cdoRequest, Receive.class);
        LambdaQueryWrapper<Receive> lqw = buildQueryWrapper(bo);
        Receive receive=super.getOne(lqw);
        return Return.valueOf(receive);
    }

    @OpenApiTag("根据ID查询用户领卡流程信息")
    public Return queryById(CDO cdoRequest) {
        Receive receive=super.getById(cdoRequest.getLongValue("id"));
        return Return.valueOf(receive);
    }

    private LambdaQueryWrapper<Receive> buildQueryWrapper(Receive receive) {
        LambdaQueryWrapper<Receive> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(Receive::getId);

        lqw.eq(receive.getId() != null, Receive::getId, receive.getId());
        lqw.like(StringUtils.isNotBlank(receive.getReceiveName()), Receive::getReceiveName, receive.getReceiveName());
        lqw.like(StringUtils.isNotBlank(receive.getReceivePhone()), Receive::getReceivePhone, receive.getReceivePhone());
        lqw.like(StringUtils.isNotBlank(receive.getReceiveIdcard()), Receive::getReceiveIdcard, receive.getReceiveIdcard());
        lqw.like(StringUtils.isNotBlank(receive.getReceiveTown()), Receive::getReceiveTown, receive.getReceiveTown());
        lqw.like(StringUtils.isNotBlank(receive.getReceiveAddress()), Receive::getReceiveAddress, receive.getReceiveAddress());
        lqw.eq(receive.getReceiveCard() != null, Receive::getReceiveCard, receive.getReceiveCard());
        lqw.like(StringUtils.isNotBlank(receive.getReceiveCardCourier()), Receive::getReceiveCardCourier, receive.getReceiveCardCourier());
        lqw.like(StringUtils.isNotBlank(receive.getReceiveCardPhone()), Receive::getReceiveCardPhone, receive.getReceiveCardPhone());
        lqw.like(StringUtils.isNotBlank(receive.getReceiveCircumference()), Receive::getReceiveCircumference, receive.getReceiveCircumference());
        lqw.like(StringUtils.isNotBlank(receive.getCreatedAt()), Receive::getCreatedAt, receive.getCreatedAt());
        lqw.like(StringUtils.isNotBlank(receive.getUpdatedAt()), Receive::getUpdatedAt, receive.getUpdatedAt());
        return lqw;
    }

    /**
     * save
     */
    @Override
    @OpenApiTag("保存用户领卡流程信息")
    public Return save(CDO cdoRequest) {
        cdoRequest.remove("createtime");
        cdoRequest.remove("updatetime");
        Receive bo = BeanUtil.toBean(cdoRequest, Receive.class);
        Return ret = this.validEntity(cdoRequest,bo,0);
        if(ret.getCode()!=0){
            return ret;
        }
        super.save(bo);
        return Return.SUCCESS;
    }
    /**
     * update
     */
    @OpenApiTag("更新用户领卡流程信息")
    public Return update(CDO cdoRequest) {
        cdoRequest.remove("createtime");
        cdoRequest.remove("updatetime");
        Receive bo = BeanUtil.toBean(cdoRequest, Receive.class);
        Return ret = this.validEntity(cdoRequest,bo,1);
        if(ret.getCode()!=0){
            return ret;
        }
        super.updateById(bo);
        return Return.SUCCESS;
    }

    /**
     * delete
     */
    @OpenApiTag("删除用户领卡流程信息")
    public Return delete(CDO cdoRequest) {
        String[] ids = cdoRequest.getStringValue("ids").split(",");
        boolean b = super.removeBatchByIds(Arrays.asList(ids));
        if(b) {
            return Return.SUCCESS;
        }
        return Return.EXCEPTION;
    }

    private Return validEntity(CDO cdoRequest,Receive entity,Integer type){
        //TODO 做一些数据校验,如唯一约束
        return Return.SUCCESS;
    }
}
